<template>
  <el-container class="home-container">
    <el-header class="header">
      <div class="header-left">
        <div class="logo">
          <img src="../assets/logo.jpg" alt="" />
        </div>
        <div class="title">后台管理系统</div>
      </div>
      <div class="header-right">
        <el-avatar
          src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
        ></el-avatar>
        <div class="userInfo">admin</div>
      </div>
    </el-header>
    <el-container>
    <el-aside :width="isCollapse?'64px':'200px'" class="aside"
      >
      <div class="toggle-button" @click="toggleCollapse">|||</div>
      <el-menu
        :default-active="activePath"
        class="el-menu-vertical-demo"
        background-color="#313743"
        text-color="#fff"
        active-text-color="#409EFF"
        unique-opened
        :collapse='isCollapse'
        :collapse-transition="false"
        router
      >
        <el-submenu :index="item.path"  v-for="item in menuList" :key="item.id" >
          <template slot="title">
            <i :class="iconsObj[item.id]"></i>
            <span>{{ item.authName}}</span>
          </template>
          <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState(subItem.path)">
            <i class="el-icon-menu"></i>
            <span slot="title">{{ subItem.authName}}</span>
          </el-menu-item>
        </el-submenu>
      </el-menu></el-aside
    >
    <el-main class="main">
      <router-view></router-view>
    </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      // 左侧菜单数组
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  computed: {},
  methods: {
    async getMenuList () {
      const { data: res } = await this.$http.get('menus', this.form)
      if (res.meta.status === 200) {
        this.menuList = res.data
      } else {
        this.$message.error(res.meta.msg)
      }
    },
    // 点击按钮切换折叠与展开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      this.activePath = activePath
      console.log(activePath)
      window.sessionStorage.setItem('activePath', activePath)
    }
  }
}
</script>
<style lang="less" scoped>
@font-face {
  font-family: bigTitle; //大标题
  src: url("../assets/锐字真言体免费商用.ttf");
}
.home-container {
  height: 100%;
  width: 100%;
}
.header {
  background-color: #363D40;
  height: 40px;
  width: 100%;
  display: flex;
  justify-content: space-between;
  .header-left {
    display: flex;
    // border: 1px solid red;
    .title {
      height: 50px;
      margin-left: 20px;
      font-size: 28px;
      font-family: bigTitle;
      color: #eee;
      line-height: 50px;
      text-align: center;
      // border: 1px solid red;
    }
    .logo {
      margin-top: 3px;
      height: 30px;
      width: 30px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
  }
  .header-right {
    padding-top: 10px;
    display: flex;
    .userInfo {
      margin-left: 20px;
      height: 40px;
      line-height: 40px;
      color: #eee;
      cursor: pointer;
    }
  }
}
.aside {
  background-color: #313743;
  height: 100%;
  // padding-top:20px ;
  overflow: auto;
  // border: 1px solid red;
}
.main{
  // border: 1px solid red;
  height: 100%;
  width: 100%;
}
.iconfont{
  margin-right: 10px;
}
.el-menu{
  border-right:none;
}
.toggle-button{
  color: #fff;
  height: 24px;
  line-height: 24px;
  background-color: #4A5064;
  text-align: center;

  cursor: pointer;
}
</style>
